<template>
  <div>
    <el-dialog
      :visible.sync="isShow"
      title="修改权限"
      width="500px"
      @close="cancelEvent"
    >
      <el-tree
        ref="tree"
        :data="treeData"
        check-strictly
        :props="{ label: 'name' }"
        show-checkbox
        node-key="id"
      />

      <template #footer>
        <div style="text-align: center">
          <el-button @click="cancelEvent">取消</el-button>
          <el-button type="primary" @click="submit">修改</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysPermission } from '@/api/premission'
import { changeData } from '@/utils/index'
import { sysRoleAssignPrem, sysRoleId } from '@/api/setting'
export default {
  data() {
    return {
      isShow: false,
      treeData: [],
      defaultKeys: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },

  methods: {
    cancelEvent() {
      this.isShow = false
    },
    async getData() {
      const res = await sysPermission()
      this.treeData = changeData(res.data, '0')
      console.log('获取用户权限', res)
    },

    async getUserInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('根据id获取用户信息', res)
    },
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('修改权限成功')
      this.cancelEvent()
    }
  }
}
</script>

<style></style>
